package com.tansuo365.test1.service.otherservice.impl;

import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.bean.otherbean.ShengouZong;
import com.tansuo365.test1.mapper.othermapper.ShengouDetailsMapper;
import com.tansuo365.test1.mapper.othermapper.ShengouZongMapper;
import com.tansuo365.test1.service.otherservice.ShengouZongService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShengouZongServiceImpl implements ShengouZongService {

    @Resource
    private ShengouZongMapper shengouZongMapper;
    @Resource
    private ShengouDetailsMapper shengouDetailsMapper;

    @Override
    public String getTodayMaxShengouDanNumber() {
        return shengouZongMapper.getTodayMaxShengouDanNumber();
    }

    @Override
    public List<ShengouZong> listShengouZong(ShengouZong shengouZong) {
        return shengouZongMapper.listShengouZong(shengouZong);
    }

    @Override
    public List<ShengouZong> listShengouZongWhereNotFull(ShengouZong shengouZong) {
        return shengouZongMapper.listShengouZongWhereNotFull(shengouZong);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return shengouZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return shengouZongMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public int insertSelective(ShengouZong shengouZong) {
        return shengouZongMapper.insertSelective(shengouZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(ShengouZong shengouZong, List<ShengouDetails> shengouDetailsList) {
//        String shenhezhuangtai = shengouZong.getShenhezhuangtai();
//        if(shenhezhuangtai.equals("已审核")){
//            shengouZong.setCandelete("2"); 审核已经从数据库默认预设 从前端按钮变更
//        }
        int insertCode = 0;
        insertCode = shengouZongMapper.insertSelective(shengouZong);
        if(!(insertCode>0)){
            return 0;
        }
        int i = 0;
        Integer zongzhangId = shengouZong.getId();
        //保存每个申购单物料
        for(ShengouDetails shengouDetails : shengouDetailsList){
            //设置对应总表id
            i++;
            shengouDetails.setZongzhangid(zongzhangId);
            shengouDetailsMapper.insertSelective(shengouDetails);
            //其他计算,如到货单这里可以增加相应库存
        }
        if(!(i==shengouDetailsList.size() && i!=0)){
            return 0;
        }
        return i;
    }

    @Override
    public ShengouZong selectByPrimaryKey(Integer id) {
        return shengouZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ShengouZong shengouZong) {
        return shengouZongMapper.updateByPrimaryKeySelective(shengouZong);
    }

    @Override
    public int updateCanDeleteByDanhao(String shengoudanhao) {
        return shengouZongMapper.updateCanDeleteByDanhao(shengoudanhao);
    }

    @Override
    public int updateCanDeleteToCan(String shengoudanhao) {
        return shengouZongMapper.updateCanDeleteToCan(shengoudanhao);
    }

    @Override
    public List<String> listShengouDanhao(Integer[] ids) {
        return shengouZongMapper.listShengouDanhao(ids);
    }
}
